from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View



# from redis import StrictRedis
class ImageCodeView(View):

    # 2.1生成图片验证码,
    def get(self,request,uuid):
        # 2.生成图片验证码,和保存图片验证码的内容
        # 2.1 生成图片验证码,
        from libs.captcha.captcha import captcha
        # generate_captcha 它返回2个值,值是图片验证码的二进制图片
        text,image = captcha.generate_captcha()

        # 2.2 保存图片验证码的内容 redis
        # 2.2.1 连接redis
        from django_redis import get_redis_connection
        redis_conn = get_redis_connection('code')
        # 2.2.2 保存数据
        # redis_conn.setex(键,过期时间,值)

        redis_conn.setex('img_%s' % uuid, 120, text)
        # 3.把图片返回给浏览器
        # application/json

        # content_type 其实就是MIME类型
        # 语法: 大类/小类
        # image 图片
        # image/jepg image/png image/gif
        # text/html     text/javascript     text/css

        # Content-Type:text/html 默认是 text/html
        # return http.HttpResponse(image)
        return http.HttpResponse(image, content_type='image/jpeg')
